<?php

class CLoader {
    public static function loadClass($class, $file = NULL)
    {
        if (class_exists($class, FALSE) || interface_exists($class, FALSE)) {
            return;
        }
		
        if(substr($class, -5) === 'Model') {
			$file = APP_PATH . '/model/' . $class . '.php';
        } elseif (substr($class, -10) === 'Controller') {
        	$file = APP_PATH . '/controller/' . $class . '.php';			
		} else {
			$file = KISS_PATH . '/library/' .  $class . '.php';
		}

        include($file);
    }

    public static function autoload($class)
    {
        try {
            @self::loadClass($class);
            return $class;
        } catch (Exception $e) {
            return FALSE;
        }
    }

    public static function registerAutoload($class = 'CLoader')
    {
        if (!function_exists('spl_autoload_register')) {
            throw new Exception('spl_autoload does not exist in this PHP installation');
        }

        self::loadClass($class);
        $methods = get_class_methods($class);
        if (!in_array('autoload', (array) $methods)) {
            throw new Exception("The class '$class' does not have an autoload method");
        }
        
        spl_autoload_register(array($class, 'autoload'));
    }
}
